/**
 * @(#) ReadVacationMediator.as
 *
 * @date		2010. 09. 09.
 * @version		1.0
 * @author		Shin, Yunwoo
 * @description	휴가 신청 내역 조회
 * 				근무관리 Presentation Logic을 관리 
 */
package com.skt.common_hr.work_nos.view
{
	import com.skt.common_hr.common.components.CommonAlert;
	import com.skt.common_hr.common.puremvc.CommonMediator;
	import com.skt.common_hr.work_nos.WorkFacade;
	import com.skt.common_hr.work_nos.model.ReqVacationProxy;
	import com.skt.common_hr.work_nos.model.vo.VacationVO;
	import com.skt.common_hr.work_nos.view.components.ReadVacationView;
	
	import flash.events.Event;
	
	import mx.managers.PopUpManager;
	
	import org.puremvc.as3.multicore.interfaces.INotification;
	
	public class ReadVacationMediator extends CommonMediator
	{
		public static const NAME:String = "ReadVacationMediator"; 

		public var baseDate:Date;	
		
		public var _savingTable:Array;
		private var _currentKey:String;
		private var GWAREKEY:String;
		
		/**
		* 생성자
		* 
		* @param
		* @return void
		*/
        public function ReadVacationMediator( viewComponent:Object )
        {
            super( NAME, viewComponent );
            view.callLater(initView);
        }
 		
		/**
		* View를 초기화 한다.
		*	- View에 기본사항을 출력하고 data를 load
		* @param
		* @return void
		*/
 		private function initView():void
 		{
			setEventListener();
 		}
		
		public function exec(date:Date, key:String):void
		{
			bActivated = true;
			baseDate = new Date(date);
			GWAREKEY = key;
			loadData();
		}		
		
		
		private function loadData():void
		{
			var vacProxy:ReqVacationProxy = facade.retrieveProxy(ReqVacationProxy.NAME) as ReqVacationProxy;
			vacProxy.load(GWAREKEY);
		}
		
		private function setEventListener():void
		{
 			// 각 버튼들의 Event Handler를 설정하고
 			view.addEventListener(ReadVacationView.DELETE, onDelete);
 			view.addEventListener(ReadVacationView.CANCEL, onCancel);
		}
		
		private function removeEventListener():void
		{
 			view.removeEventListener(ReadVacationView.DELETE, onDelete);
 			view.removeEventListener(ReadVacationView.CANCEL, onCancel);
		}
		
		
 		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Process MVC Messages
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		* 본 Class에서 처리해야할 Noti들을 등록한다.
		* 
		* @param
		* @return void
		*/
		override public function listNotificationInterests():Array 
		{
			return [
					WorkFacade.LOAD_REQ_VACATION_COMPLETE,
					WorkFacade.LOAD_REQ_VACATION_FAILED,
					WorkFacade.DELETE_REQ_VACATION_COMPLETE,
					WorkFacade.DELETE_REQ_VACATION_FAILED,
					];
		}
		

		/**
		* Noti별로 처리한다.
		* 
		* @param
		* @return void
		*/
		override public function handleNotification(note:INotification):void
		{
			if(bActivated) {
				switch(note.getName()) 
				{
					case WorkFacade.LOAD_REQ_VACATION_COMPLETE:
						view.reqList = (note.getBody() as ReqVacationProxy).reqVacationList;
						break;
					case WorkFacade.DELETE_REQ_VACATION_FAILED:
						CommonAlert.showMessage(commonFacade.bundle, "("+note.getBody().retCode+")"+note.getBody().retText, null, "RFC 오류", view);
						break;
					case WorkFacade.DELETE_REQ_VACATION_COMPLETE:
						closeView();
						break;
					default:
						//trace(this.instanceID+"에서 처리 안된 Noti가 검출됨 : " + note.getBody());
						break;
				}
			}
		}
		
		private function closeView(bCancel:Boolean = false):void
		{
 			PopUpManager.removePopUp(view);
 			bActivated = false;
			//facade.sendNotification((bCancel) ? WorkFacade.CANCEL_REQ_WORKTIME : WorkFacade.SUCCEED_REQ_WORKTIME, this);
			facade.sendNotification(WorkFacade.CANCEL_READ_VACATION, this);
		}
		

 		private function onDelete(event:Event):void
 		{
 			var proxy:ReqVacationProxy = facade.retrieveProxy(ReqVacationProxy.NAME) as ReqVacationProxy;
 			proxy.del([{GWAREKEY: GWAREKEY}]);
 		}
 		
 		private function onCancel(event:Event):void
 		{
 			closeView(true);
 			
 		}
 		
  		

 		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Utility Functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
        private function get view():ReadVacationView
        {
            return viewComponent as ReadVacationView;
        }
	}
}